<?php
class Email {

    public $mailer;
    public $from = array();
    public $to = array();
    public $cc = array();
    public $bcc = array();
    public $subjectPrefix;
    public $subject = '';
    public $attachments = array(); //array('filename' => array('inline'=>true, 'path'=> 'path/to/file.png', 'contentType'=> 'application/pdf', 'body'=>'dynamic/content'))
    public $message;
    public $failures;
    public $params = array();
    public $urls = array();
    public $language;
    public $view = 'default';

    function __construct() {
        $this->mailer = new YiiMailMessage;
        $this->from = array(Yii::app()->params['defaultEmail'] => Yii::app()->params['defaultSender']);
        $this->bcc = array(Yii::app()->params['notificationEmail'] => Yii::app()->params['notificationName']);
        $this->subjectPrefix = Yii::app()->params['mailSubjectPrefix'];
        $this->language = Yii::app()->params['defaultLanguage'];
    }

    public function setMail() {
        Yii::app()->controller->initEmail();
        Yii::app()->controller->edmSubject=$this->subject;
        Yii::app()->controller->edmUrl='online url';
        
        $this->params['message'] = $this->message;
        $this->params['urls'] = $this->urls;

        //echo Yii::app()->controller->renderPartial(implode('.', array('edmViewPath', $this->language, $this->view)), $this->params, true); exit;

        $this->mailer->setSubject($this->subjectPrefix . $this->subject)
                ->setFrom($this->from)
                ->setTo($this->to)
                ->setCc($this->cc)
                ->setBcc($this->bcc)
                //->setBody($this->message, 'text/html')
                ->setBody(Yii::app()->controller->renderPartial(implode('.', array('edmViewPath', $this->language, $this->view)), $this->params, true), 'text/html')
        ;

        if (!empty($this->attachments)) {
            $this->attachFiles();
        }	
    }

    private function attachFiles() {
        foreach ($this->attachments as $filename => $config) {

            if (isset($config['path'])) {
                $attachment = Swift_Attachment::fromPath($config['path'])->setFilename($filename);
            } else {
                $attachment = Swift_Attachment::newInstance()->setFilename($filename);
                if (isset($config['body']))
                    $attachment->setBody($config['body']);
            }

            if (isset($config['inline']) && $config['inline'])
                $attachment->setDisposition('inline');
            if (isset($config['contentType']))
                $attachment->setContentType($config['contentType']); //'application/pdf'

            $this->mailer->attach($attachment);
        }//end foreach
    }

    public function notifyAdmin() {
        if(!Yii::app()->params['emailMode']) return;
        $this->to = array(Yii::app()->params['adminEmail'] => Yii::app()->params['adminName']);
        $this->setMail();
        return Yii::app()->mail->send($this->mailer, $this->failures);
    }

    public function send() {
        if(!Yii::app()->params['emailMode']) return;
        $this->setMail();
        return Yii::app()->mail->send($this->mailer, $this->failures);
    }

    public function sendBulk() {
        if(!Yii::app()->params['emailMode']) return;
        $this->setMail();
        return Yii::app()->mail->batchSend($this->mailer, $this->failures);
    }

    public function sendSimple() {
        if(!Yii::app()->params['emailMode']) return;
        return Yii::app()->mail->sendSimple($this->from, $this->to, $this->subject, $this->body);
    }

}//end class